const News = tableMap.get("news");
const helper = require("./helper");

function formatNews(form) {
	const title = String.asString(form.title).trim();
	if (!title) {
		Throw("ref", "新闻标题不可为空")
	}
	const author = String.asString(form.author).trim();

	const imgurl = String.asString(form.imgurl);
	// if (imgurl && $$.isUrl(imgurl)) {
	// 	Throw("ref", "封面的图片地址有误")
	// }
	if (String.isString(form.section)) {
		var section_id = form.section;
	} else if (form.section && form.section.$ref === "session") {
		section_id = form.section.$id;
	}
	const section = section_id ? ["DBRef", "@", "section", section_id] : null;

	const html = $$.xss(String.asString(form.html));

	const createTime = +form.createTime || Date.now();

	return {
		title: title,
		author: author,
		imgurl: imgurl,
		section: section,
		html: html,
		updateTime: Date.now(),
		createTime: createTime,
	};
};
exports.install = install;

function install(socket) {
	function populatexxNews(find_news_promise) {
		return find_news_promise.then(co.wrap(function*(news_list) {
			return yield news_list.map(news => socket.mongodbPupulateAll(news));
		}));
	}
	return {
		prefix: "/news",
		get: {
			"/detail_by_id/:news_id": [{
				des: "根据新闻ID获取新闻内容"
			}, function*() {
				const news_id = this.params.news_id;
				if (!news_id) {
					Throw("type", "缺少要获取的新闻编号");
				}

				this.body = yield News.findOne({
					_id: ["ObjectID", "@", news_id]
				})
			}],
			"/list_by_section_id/:section_id": [{
				des: "获取指定版块的新闻列表",
				emit_with: ["params", "query"]
			}, function*() {
				const section_id = this.params.section_id;
				if (!section_id) {
					Throw("type", "缺少要指定的版块编号");
				}
				const query = this.query;

				this.body = yield helper.findWithPageInfo(News, {
					"section.$id": section_id
				}, query.num, query.page, {
					project: {
						html: 0
					}
				});
			}],
			"/list_all": [{
				des: "获取新闻列表",
				emit_with: ["query"]
			}, function*() {
				const query = this.query;

				this.body = yield helper.findWithPageInfo(News, {}, query.num, query.page, {
					project: {
						html: 0
					}
				});
			}]
		},
		post: {
			"/create": [{
				des: "管理员发表新闻",
				emit_with: ["form", "session"]
			}, function*() {
				const admin = yield this.admin_loginer();
				const form = this.form;

				yield News.insertOne(formatNews(form));

				this.body = (yield helper.populateList(News, News.find({}).limit(1).sort([["createTime", -1]]).toArray()))[0];
			}]
		},
		put: {
			"/update": [{
				des: "管理员修改新闻",
				emit_with: ["form", "session"]
			}, function*() {
				const admin = yield this.admin_loginer();
				const form = this.form;

				const _id = form._id;
				if (!_id) {
					Throw("type", "缺少要修改的新闻编号");
				}

				const update_form = formatNews(form);
				delete update_form.createTime;

				yield News.updateOne({
					_id: ["ObjectID", "@", _id]
				}, {
					$set: update_form
				});

				this.body = yield News.findOne({
					_id: ["ObjectID", "@", _id]
				});
			}]
		},
		delete: {
			"/remove/:news_id": [{
				des: "管理员删除新闻",
				emit_with: ["params", "session"]
			}, function*() {
				const admin = yield this.admin_loginer();
				const news_id = this.params.news_id;
				if (!news_id) {
					Throw("type", "缺少要删除的新闻编号");
				}

				this.body = yield News.deleteOne({
					_id: ["ObjectID", "@", news_id]
				});
			}]
		}
	};
};